#!/usr/bin/env bash

# 阻止 source
if ! [[ ${BASH_SOURCE[0]} == $0 ]];then
  agg log.warn "不允许使用 source 执行脚本"
  exit 127
fi

# 定义
json=$BASH_SANDBOX_ROOTDIR/etc/data/yunzai.v4.index.json
SH_0=$(basename $0)

# 检查
! [ -f $json ] &&
  agg log.err "$SH_0: 找不到 $json" &&
  exit 1

# 解析
Yz_Docs=($(jq -r '.docs | keys[]' $json))
Yz_Repo=($(jq -r '.repositories | keys[]' $json))
Yz_Plug=($(jq -r '.plugins | keys[]' $json))
Yz_Bots=($(jq -r '.bots | keys[]' $json))


case $1 in
  bot*)
    SH_1_Repo=$(jq -r ".bots.[\"$2\"][\"repositorie\"]" $json)
    SH_1_Branch=$(jq -r ".bots.[\"$2\"][\"branch\"]" $json)
    ii=0
    for i in ${Yz_Repo[@]};do
      [[ $i == $SH_1_Repo ]] &&
        SH_1_Repo=$(jq -r ".repositories | .[\"$i\"]" $json) &&
        let ii++
    done
    [[ ! $ii == 0 ]] &&
      agg log.info "git clone --depth=1 -b $SH_1_Branch $SH_1_Repo $HOME/$2"
      
  ;;
esac
    